<?php
class KechengAction extends CommonAction{
	
 	 private  $table_name;
   	 function __construct(){
   	 	
   	 	$this->table_name = "Kecheng";
   	 	parent::__construct();
   	 }
	

  	function  index(){
      
					import('ORG.Util.Page');

					 $count=M($this->table_name)->count();// 查询总数据记录

					 $Page = new Page($count,C('PAGESIZE'));

					 $show= $Page->show();

	                    $temp = M($this->table_name)->order("createtime DESC")->limit($Page->firstRow.','.$Page->listRows)->select();
                        foreach ($temp as $v){
                        	  $v['name'] = M('Category')->where("id=".$v['cid'])->getField('name');
                        	  $list[] =$v;
                        	
                        }
                           $temp =M("Category")->select();
				   	  		$cate = cate_tree_list($temp,0);
				   	  		$this->assign("cate",$cate);
	                   
	                      $this->assign("list",$list);

						  $this->assign("page",$show);

                          $this->display();

       }

       function add(){
  	  	
   	  	if(isset($_POST['submit'])){
   	  	     $data = $_POST;
           $data['createtime']=mktime();
           $data['status']= 1 ;
           $username = $_POST['username'];
           $password = md5($_POST['password']);
           $uid = M('User')->add(array('username'=>$username,'password'=>$password,'logintime'=>mktime(),'usertype'=>1,'status'=>1,'nickname'=>$_POST['title']));
           if(isset($uid)){
              	  $data['uid'] = $uid;	
              	     
           	      if(M($this->table_name)->add($data)){
		   	  	    	$this->success("操作成功！",U('index'));
		   	  	    }else{
		   	  	    	
		   	  	    	$this->error("操作失败");
		   	  	    }
           	
           } 

   	  	}else{
   	  		$temp =M("Category")->select();
   	  		$list = cate_tree_list($temp,0);
   	  		$this->assign("list",$list);
   	  		$this->display();
   	  	}
       	
       }

  	   /**

  	    * 

  	    * 编辑文章

  	    */

    	function  edit(){
	              $id= htmlspecialchars($_GET['id']);
	              if(is_numeric($id)){
	              	  if(isset($_POST['submit'])){
	              	  	  $data = $_POST;
	              	  	  $data['createtime']=mktime();
	              	  	  M($this->table_name)->where("id=".$id)->save($data);
	              	  	  $this->success("更新成功！",U('index'));
	              	  }else{
	              	  	$temp =M("Category")->select();
   	  		            $list = cate_tree_list($temp,0);
	              	  	$this->li =M($this->table_name)->where("id=".$id)->find();
	              	  	$this->assign("list",$list);
	              	  	$this->display();
	              	  }
	              	
	              }
  	 }


  	 /**

  	  * 删除

  	  * 

  	  */

  	 function del(){

	        $qstr = $_GET['qstr'];
  		  	$data=M($this->table_name);  		  
			if(isset($qstr)){
			   $arr=explode(",",$qstr);

			   foreach($arr as $val){

			     $data->where("id=$val")->delete();
			       
			   }
			   $this->success("删除成功！",U('index'));			

			}else{

			  if(isset($_GET['id'])){

			  $data->where("id=".$_GET['id'])->delete();

			   $this->success("删除成功！",U('index'));

			  }else{

			  

			   $this->error("请选择要删除的ID");

			  }

			}

  	 }
  	 
  	function tixing(){
  		 $id = empty($_GET['id'])?Exit():$_GET['id'];//课程ID
  		if(isset($_POST['submit'])){
  			$data = $_POST;
  			$data['start_time']= strtotime($_POST['start_time']); 
  			$data['kid'] = $_GET['id'];
  			  if(M($this->table_name.'Tixing')->add($data)){
  			  	
  			  	$this->success("添加提醒成功！");
  			  }
  			   
  		}else{
  			$this->list =M($this->table_name.'Tixing')->where("kid=".$id)->order("start_time ASC")->select();	 
  			$this->assign("id",$_GET['id']); 
  			$this->display();
  		}
  	} 
  	
  	
   	
  function dtixing(){
  	 $id = empty($_GET['id'])?Exit():$_GET['id'];//课程ID
  	  M("kecheng_tixing")->where("id=".$id)->delete();
  	  $this->success("删除成功！");
  }	
  
  
  function timu(){
    $id = empty($_GET['id'])?exit():$_GET['id'];//课程ID
  	if(isset($_POST['submit'])){
  		 
  		
  	}else{
  		 $this->list =M('Timu')->where("kid=".$id)->select(); 
  		 $this->display();
  	}
    	
  }
  
  
	
}
?>